package com.sfac.controller;

import com.github.pagehelper.PageInfo;
import com.sfac.common.vo.Result;
import com.sfac.common.vo.Search;
import com.sfac.service.CityService;
import com.sfac.test.City;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * City Controller
 */
@RestController
@RequestMapping("/api/test")
public class CityController {

	@Autowired
	private CityService cityService;

	/**
	 * 127.0.0.1/api/city ---- post
	 * {"cityName":"dream","localCityName":"梦想之城","countryId":522}
	 */
	@PostMapping(value = "/city", consumes = "application/json")
	public Result<City> insertCity(@RequestBody City city) {
		return cityService.insertCity(city);
	}

	/**
	 * 127.0.0.1/api/city ---- put
	 * cityName=dream1,localCityName=梦想之城1,cityId=1890
	 */
	@PutMapping(value = "/city", consumes = "application/json")
	public Result<City> updateCity(@RequestBody City city) {
		return cityService.updateCity(city);
	}

	/**
	 * 127.0.0.1/api/city/2257 ---- delete
	 */
	@DeleteMapping(value = "/city/{cityId}")
	public Result<Object> deleteCityByCityId(@PathVariable int cityId) {
		return cityService.deleteCityByCityId(cityId);
	}

	/**
	 * 127.0.0.1/api/city/2257 ---- get
	 */
	@GetMapping(value = "/city/{cityId}")
	public City getCityByCityId(@PathVariable int cityId) {
		return cityService.getCityByCityId(cityId);
	}

	/**
	 * 127.0.0.1/api/cities/522 ---- get
	 */
	@GetMapping(value = "/cities/{countryId}")
	public List<City> getCitiesByCountryId(@PathVariable int countryId) {
		return cityService.getCitiesByCountryId(countryId);
	}

	/**
	 * 127.0.0.1/api/cities ---- post
	 * {"currentPage":1,"pageSize":5,"sort":"cityId","direction":"desc","keyword":""}
	 */
	@PostMapping(value = "/cities", consumes = "application/json")
	public PageInfo<City> getCitiesBySearch(@RequestBody Search search) {
		return cityService.getCitiesBySearch(search);
	}

	/**
	 * 127.0.0.1/api/cities/v2 ---- post
	 * [{"cityName":"dream11","localCityName":"梦想之城11","countryId":522},
	 * {"cityName":"dream22","localCityName":"梦想之城22","countryId":522}]
	 */
	@PostMapping(value = "/cities/v2", consumes = "application/json")
	public Result<Object> insertBatchCities(@RequestBody List<City> cities) {
		return cityService.insertBatchCities(cities);
	}
}
